home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1999 May / SGI IRIX 6.5 Applications 1999 May.iso / dist / nss_fasttrack.idb / var / netscape / fasttrack / js / samples / compiler / Calljsac.java.z / Calljsac.java
Text File  |  1998-10-13  |  6KB  |  184 lines

  1. package compiler;
  2. import netscape.javascript.*;
  3. //import netscape.server.serverenv.*;
  4.  
  5. import java.io.FileInputStream;
  6. import java.io.DataInputStream;
  7. import java.io.File;
  8.  
  9. /**
  10. * A class to execute the LiveWire commandline compiler from Java
  11. */
  12.  
  13. public class Calljsac
  14. {
  15.     /**************************************
  16.     * A convenience wrapper version       *
  17.     * called from server-side JavaScript. *
  18.     ***************************************/
  19.     public static String exec( String jsac, String path,
  20.                                String filelist, String outfile,
  21.                                boolean verbose )
  22.     {
  23.         int KNOWN_PARAMS = 4;
  24.         String[] params = new String[KNOWN_PARAMS+(verbose?1:0)];
  25.  
  26.           // Start building a command line
  27.             
  28.           // Use an include file that contains the list of source files to be compiled
  29.         params[0] = "-f";
  30.         params[1] = filelist;
  31.         // Designate name of .web file
  32.         params[2] = "-o";
  33.         params[3] = outfile;
  34.         // If verbose = true, add as a parameter
  35.         int i = KNOWN_PARAMS;
  36.         if( verbose )
  37.             params[i++] = "-v";
  38.  
  39.         return exec( jsac, path, params, filelist );
  40.     }
  41.  
  42.     public static String exec( String jsac, String path, String[] params, String filelist )
  43.     {
  44.           // Start a string that will hold all output from this routine and the compiler
  45.         String retString = "-1#";
  46.         
  47.         // Generate a file name for the temporary file that will hold output from the compiler
  48.         String tempFilename = generateTempFilename();
  49.         
  50.           // Build the command line, e.g. "jsac -v -o output.web ... "
  51.         String[] cmdArray = buildCmdArray( jsac, path, params, tempFilename );
  52.  
  53.           // Create a handle to the temporary file that holds the output from the compiler
  54.         File tempFile = new File( path, tempFilename );
  55.  
  56.         if( tempFile.exists() )
  57.             tempFile.delete();
  58.  
  59.         try
  60.         {
  61.             /* For debugging only
  62.             int numArgs = cmdArray.length;
  63.             System.out.println("Command is " );
  64.             String temp = "";
  65.             for (int i=0; i<numArgs; i++) {
  66.                 temp += cmdArray[i] + " ";
  67.             }
  68.                 System.out.println( temp );
  69.             // -----------------------------
  70.                 */
  71.  
  72.                 // Launch the compiler
  73.             Process p = Runtime.getRuntime().exec( cmdArray );
  74.             p.waitFor();
  75.             
  76.             // An exit value of 0 indicates success, anything else is an error
  77.                 int retValue = p.exitValue();
  78.                 // Begin building our return string
  79.             retString = retValue + "#";
  80.             p.destroy();
  81.  
  82.                 // If an error occurred, try and add informative error messages to the return string
  83.                 // to help the user identify the problem.
  84.                 if ( retValue != 0 ) 
  85.                 {
  86.                     retString += catchFileErrors(path, filelist, tempFile);    
  87.                 } 
  88.                 
  89.                 // Read the compiler output file, if any, and add to the return string
  90.             if( tempFile.exists() )
  91.             {
  92.                 StringBuffer buf = null;
  93.                 DataInputStream s = null;
  94.                 try {
  95.                     buf = new StringBuffer();
  96.                     s = new DataInputStream(new FileInputStream(tempFile));
  97.                 }
  98.                 catch ( Exception e )
  99.                 {
  100.                     retString += "EXCEPTION in streaming\n";
  101.                     retString += "Check file permissions for directory " + path + "\n";
  102.                 }
  103.                 if( null != s )
  104.                 {
  105.                     String str;
  106.                     while( null != (str = s.readLine()) )
  107.                     {
  108.                         buf.append(str);
  109.                         buf.append("\n");
  110.                     }
  111.                 }
  112.                 s.close();
  113.                 retString += buf;
  114.             }
  115.         }
  116.         catch( Exception e )
  117.         {
  118.             retString += "EXCEPTION: -1#\n";
  119.             retString += "Check your system path and be sure the path to the jsac compiler is correct.\n";
  120.         }
  121.  
  122.           // Clean up our temporary compiler output file
  123.         if( tempFile.exists() )
  124.             tempFile.delete();
  125.  
  126.           // Pass back results as a string
  127.         return retString;
  128.     }
  129.  
  130.     private static String generateTempFilename()
  131.     {
  132.         return "__jsac.tmp";  // XXX this should be based on a random number..
  133.     } // END generateTempFilename
  134.  
  135.      private static String catchFileErrors (String path, String filelist, File tempFile ) 
  136.      {
  137.              String retString = "";  // local variable to build result/error string
  138.      
  139.             // Create a handle to filelist which contains list of files to be compiled        
  140.             File     filelistFileHandle = new File(path, filelist);
  141.                     
  142.             // Does the file indicated by 'filelist' exist?
  143.             if (!filelistFileHandle.exists()) {
  144.                 retString += "Cannot find list of files to be compiled in file '" + filelist + "' \n";
  145.                 retString += "in path " + path + ".\n";
  146.                 retString += "Check to be sure the path is correct and that the file exists.\n";
  147.             } else if (!filelistFileHandle.canRead()) {
  148.                 // Otherwise, do we have permission to read this file?
  149.                     retString += "No permission to read file list '" + filelist + "'\n";
  150.                     retString += "in path " + path + ".\n";
  151.                     retString += "Check file and directory permissions.\n";
  152.             } else if ((tempFile.exists()) && (!tempFile.canWrite())) {
  153.                 // Otherwise, do we have permission to write our temporary output file to the 'path'?
  154.                     retString += "No permission to write compiler output errors to temporary file \n";
  155.                     retString += "in path " + path + ".\n";
  156.                     retString += "Check file and directory permissions.\n";
  157.             }          
  158.      
  159.             return retString;
  160.             
  161.      } // END catchFileErrors
  162.  
  163.     private static String[] buildCmdArray( String jsac, String path,
  164.                                            String[] params, String tempFilename )
  165.     {
  166.         String[] cmd = new String[params.length + 5];
  167.  
  168.           // Build the start of the command line
  169.         cmd[0] = jsac;
  170.         cmd[1] = "-p";
  171.         cmd[2] = path;
  172.         cmd[3] = "-r";
  173.         cmd[4] = tempFilename;
  174.  
  175.           // Add other parameters 
  176.         for( int i = 0; i < params.length; i++ )
  177.              cmd[5+i] = params[i];  
  178.               
  179.         return cmd;
  180.     } // END buildCmdArray
  181.  
  182. } // END Class Calljsac
  183.  
  184.